package com.droidexchange.util;

import java.util.ArrayList;
import android.text.SpannableStringBuilder;
import android.text.style.CharacterStyle;

public class StringFormatter
{
	public static final int		TYPE_HTML		= 0;
	public static final int		TYPE_CODE		= 1;

	private static final String	TAG_CODE_START	= "<pre><code>";
	private static final String	TAG_CODE_END	= "</code></pre>";

	public class StringElem
	{
		public CharSequence	cs;
		public int				type;

		public StringElem(String _cs, int _type)
		{
			cs = _cs;
			type = _type;
		}
	}

	public ArrayList<StringElem> format(String text)
	{
		ArrayList<StringElem> elems = new ArrayList<StringFormatter.StringElem>();
		try
		{
			String startToken = TAG_CODE_START;
			String endToken = TAG_CODE_END;

			int startTokenLen = startToken.length();
			int endTokenLen = endToken.length();
			int start, end;

			while (text.length() > 0)
			{
				start = text.toString().indexOf(startToken);
				end = text.toString().indexOf(endToken, start - startTokenLen);

				StringElem elem = null;
				boolean bIsLastIter = false;
				if (start < 0 || end < 0)
				{
					// for the rest of the text, which is html.
					// this helps to stop the iterations.
					bIsLastIter = true;
					start = text.length();
					end = text.length() - endTokenLen;
				}

				String htmlText = (String) text.subSequence(0, (bIsLastIter ? text.length() : start));
				elem = new StringElem(htmlText, TYPE_HTML);
				elems.add(elem);

				if (!bIsLastIter)
				{
					String codeText = (String) text.subSequence(start + startTokenLen, end);
					elem = new StringElem(codeText, TYPE_CODE);
					elems.add(elem);

					text = text.substring(end + endTokenLen, text.length());
				}
				else
					text = "";
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

		return elems;
	}

	/**
	 * Given either a Spannable String or a regular String and a token, apply
	 * the given CharacterStyle to the span between the tokens, and also remove
	 * tokens.
	 * <p>
	 * For example, {@code setSpanBetweenTokens("Hello ##world##!", "##",
	 * new ForegroundColorSpan(0xFFFF0000));} will return a CharSequence
	 * {@code "Hello world!"} with {@code world} in red.
	 * 
	 * @param text
	 *            The text, with the tokens, to adjust.
	 * @param startToken
	 *            The token string; there should be at least two instances of
	 *            token in text.
	 * @param cs
	 *            The style to apply to the CharSequence. WARNING: You cannot
	 *            send the same two instances of this parameter, otherwise the
	 *            second call will remove the original span.
	 * @return A Spannable CharSequence with the new style applied.
	 * 
	 * @see http 
	 *      ://developer.android.com/reference/android/text/style/CharacterStyle
	 *      .html
	 */
	public static CharSequence setSpanBetweenTokens(CharSequence text, String startToken, String endToken, CharacterStyle... cs)
	{
		// Start and end refer to the points where the span will apply
		int startTokenLen = startToken.length();
		int endTokenLen = endToken.length();
		int start = text.toString().indexOf(startToken) + startTokenLen;
		int end = text.toString().indexOf(endToken, start);

		if (start > -1 && end > -1)
		{
			// Copy the spannable string to a mutable spannable string
			SpannableStringBuilder ssb = new SpannableStringBuilder(text);
			for (CharacterStyle c : cs)
				ssb.setSpan(c, start, end, 0);

			// Delete the tokens before and after the span
			ssb.delete(end, end + endTokenLen);
			ssb.delete(start - startTokenLen, start);

			text = ssb;
		}

		return text;
	}
}
